package com.example.manager.service.impl;

import com.example.manager.mapper.ProductMapper;
import com.example.manager.service.ProductService;
import com.example.manager.domain.Product;
import com.example.manager.pojo.Code;
import com.example.manager.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Result<List<Product>> selectProducts() {
        List<Product> products = productMapper.selectProducts();
        if (products == null) {
            return new Result<>(Code.RESPONSE_CODE_FAIL, "当前没有商品");
        }
        return new Result<List<Product>> (products, Code.RESPONSE_CODE_SUCCESS, "查询成功");
    }

    @Override
    public Result insertProduct(Product product) {
        if ("".equals(product.getProduct_name())) {
            return new Result<>(Code.RESPONSE_CODE_FAIL, "信息不完整");
        }
        productMapper.insertProduct(product);
        return new Result<>(Code.RESPONSE_CODE_SUCCESS, "添加成功");
    }

    @Override
    public Result updateProduct(Product product) {
        if ("".equals(product.getProduct_name())) {
            return new Result<>(Code.RESPONSE_CODE_FAIL, "信息不完整");
        }
        productMapper.updateProduct(product);
        return new Result<>(Code.RESPONSE_CODE_SUCCESS, "修改成功");
    }

    @Override
    public Result deleteProduct(Integer productId) {
        //enable为0的商品不会展示在客户端
        productMapper.updateProductEnable((short) 0, productId);
        return new Result<>(Code.RESPONSE_CODE_SUCCESS, "删除成功");
    }
}
